package com.x.动态代理.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * ClassName: MyAdvice
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.09.04 17:08
 */

/**
 * TODO:增强方法中获取目标方法信息
 * 1、全部增强方法中，获取目标方法的信息（方法名，参数，访问修饰符，所属的类的信息...）
 * (JoinPoint jointPoint)import org.aspectj.Lang.JoinPoint;
 * joinPoint包含目标方法的信息！
 *
 * 2、返回的结果  @AfterReturning
 * 方法形参：Object result 接收返回结果
 * @AfterReturning (value = "execution(* com..impl.*.*(..))", returning = "形参名即可")
 *
 * 3、异常的信息  @AfterThrowing
 * 方法形参：Throwable t 接收异常信息
 * @AfterThrowing (value = "execution(* com..impl.*.*(..))", throwing = "形参名即可")
 *
 */
@Component
@Aspect
public class MyAdvice {



    @Before("com.x.动态代理.pointCut.pointCut.pc()")
    public void start(JoinPoint joinPoint){ //连接点形参
        //获取方法所属类
        String simpleName = joinPoint.getTarget().getClass().getSimpleName();

        //获取方法名
        String methodName = joinPoint.getSignature().getName();

        //获取参数列表
        Object[] args = joinPoint.getArgs();

    }

    @AfterReturning(value = "com.x.动态代理.pointCut.pointCut.pc()", returning = "result") //配置切点(切点表达式)，和returning="result"接收返回值
    public void afterReturning(JoinPoint joinPoint, Object result){
        //获取方法所属类
        String simpleName = joinPoint.getTarget().getClass().getSimpleName();

        //获取方法名
        String methodName = joinPoint.getSignature().getName();

        //获取参数列表
        Object[] args = joinPoint.getArgs();

    }

    @After("com.x.动态代理.pointCut.pointCut.pc()")
    public void after(JoinPoint joinPoint){
        //获取方法所属类
        String simpleName = joinPoint.getTarget().getClass().getSimpleName();

        //获取方法名
        String methodName = joinPoint.getSignature().getName();

        //获取参数列表
        Object[] args = joinPoint.getArgs();

    }

    @AfterThrowing(value = "com.x.动态代理.pointCut.pointCut.pc()", throwing = "throwable") //配置切点，和throwing="throwable"接收异常类型
    public void afterThrowing(JoinPoint joinPoint, Throwable throwable){
        //获取方法所属类
        String simpleName = joinPoint.getTarget().getClass().getSimpleName();

        //获取方法名
        String methodName = joinPoint.getSignature().getName();

        //获取参数列表
        Object[] args = joinPoint.getArgs();

    }
}
